import React from 'react';
import { Link } from 'react-router-dom';
import styled from 'styled-components';
import { Rate, Tag, Badge, Tooltip } from 'antd';
import { HeartOutlined, HeartFilled, EnvironmentOutlined, CheckCircleOutlined } from '@ant-design/icons';
import { formatPrice } from '../../utils/priceUtil';
import { Hotel } from '../../api/types';

// 卡片容器
const CardContainer = styled.div`
  width: 100%;
  background-color: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
  transition: all 0.3s;
  margin-bottom: 24px;
  
  &:hover {
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
    transform: translateY(-6px);
  }
`;

// 卡片内容
const CardContent = styled.div`
  display: flex;
  flex-direction: column;
`;

// 酒店图片
const HotelImage = styled.div<{ imageUrl: string }>`
  width: 100%;
  height: 220px;
  background-image: url(${props => props.imageUrl});
  background-size: cover;
  background-position: center;
  position: relative;
`;

// 收藏按钮
const FavoriteButton = styled.div`
  position: absolute;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  transition: all 0.2s;
  
  &:hover {
    transform: scale(1.1);
    background-color: rgba(255, 255, 255, 1);
  }
  
  .anticon {
    font-size: 20px;
    color: #ff4d4f;
  }
`;

// 酒店信息
const HotelInfo = styled.div`
  flex: 1;
  padding: 18px;
  display: flex;
  flex-direction: column;
`;

// 酒店名称
const HotelName = styled.h3`
  margin: 0 0 10px;
  font-size: 18px;
  font-weight: 600;
  color: #262626;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
`;

// 酒店评分
const RatingWrapper = styled.div`
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  
  .rating-score {
    font-weight: 600;
    color: #fa8c16;
    margin-left: 8px;
  }
`;

// 酒店地址
const HotelAddress = styled.div`
  margin-bottom: 12px;
  color: #5a5a5a;
  font-size: 14px;
  display: flex;
  align-items: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  
  .anticon {
    margin-right: 5px;
    color: #1890ff;
    font-size: 14px;
    flex-shrink: 0;
  }
`;

// 酒店标签
const TagsContainer = styled.div`
  margin-top: 8px;
  margin-bottom: 12px;
  
  .ant-tag {
    margin-right: 6px;
    margin-bottom: 6px;
    border-radius: 4px;
    padding: 0 8px;
    font-size: 12px;
  }
`;

// 酒店价格信息
const PriceSection = styled.div`
  margin-top: auto;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  border-top: 1px solid #f0f0f0;
  padding-top: 14px;
  margin-top: 12px;
`;

// 价格显示
const Price = styled.div`
  .original-price {
    color: #8c8c8c;
    text-decoration: line-through;
    font-size: 14px;
    margin-right: 8px;
  }
  
  .current-price {
    color: #ff4d4f;
    font-size: 24px;
    font-weight: 600;
  }
  
  .price-unit {
    font-size: 14px;
    color: #8c8c8c;
    margin-left: 2px;
  }
`;

// 预订按钮
const BookButton = styled(Link)`
  padding: 8px 20px;
  background-color: #1890ff;
  color: #fff;
  border-radius: 6px;
  text-align: center;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
  
  &:hover {
    background-color: #40a9ff;
    color: #fff;
  }
`;

// 折扣标签
const DiscountBadge = styled(Badge)`
  position: absolute;
  top: 12px;
  left: 12px;
  
  .ant-badge-count {
    background-color: #ff4d4f;
    font-size: 14px;
    font-weight: 600;
    padding: 0 10px;
    height: 24px;
    line-height: 24px;
    border-radius: 12px;
    box-shadow: 0 2px 6px rgba(255, 77, 79, 0.3);
  }
`;

interface HotelCardProps {
  hotel: Hotel;
  isFavorite?: boolean;
  onToggleFavorite?: (hotelId: string, isFavorite: boolean) => void;
}

const HotelCard: React.FC<HotelCardProps> = ({ 
  hotel, 
  isFavorite = false,
  onToggleFavorite
}) => {
  const handleFavoriteClick = (e: React.MouseEvent) => {
    e.preventDefault();
    if (onToggleFavorite) {
      onToggleFavorite(hotel.id, !isFavorite);
    }
  };
  
  return (
    <CardContainer>
      <CardContent>
        <HotelImage imageUrl={hotel.imageUrl}>
          <FavoriteButton onClick={handleFavoriteClick}>
            {isFavorite ? <HeartFilled /> : <HeartOutlined />}
          </FavoriteButton>
          
          {hotel.discount && (
            <DiscountBadge 
              count={`${hotel.discount}折`}
              overflowCount={999}
            />
          )}
        </HotelImage>
        
        <HotelInfo>
          <HotelName>
            <Tooltip title={hotel.name}>
              {hotel.name}
            </Tooltip>
          </HotelName>
          
          <RatingWrapper>
            <Rate 
              disabled 
              defaultValue={hotel.rating} 
              allowHalf 
              style={{ fontSize: 14, color: '#faad14' }} 
            />
            <span className="rating-score">{hotel.rating.toFixed(1)}</span>
          </RatingWrapper>
          
          <HotelAddress>
            <EnvironmentOutlined />
            <Tooltip title={hotel.address}>
              {hotel.address}
            </Tooltip>
          </HotelAddress>
          
          <TagsContainer>
            {hotel.tags.map((tag, index) => (
              <Tag key={index} color={index % 3 === 0 ? "blue" : index % 3 === 1 ? "cyan" : "geekblue"}>
                {tag}
              </Tag>
            ))}
          </TagsContainer>
          
          <PriceSection>
            <Price>
              {hotel.discount && (
                <span className="original-price">
                  {formatPrice(hotel.price)}
                </span>
              )}
              <span className="current-price">
                {hotel.discount 
                  ? formatPrice(hotel.price * (hotel.discount / 10)) 
                  : formatPrice(hotel.price)
                }
              </span>
              <span className="price-unit">起/晚</span>
            </Price>
            
            <BookButton to={`/hotel/${hotel.id}`}>
              查看详情
            </BookButton>
          </PriceSection>
        </HotelInfo>
      </CardContent>
    </CardContainer>
  );
};

export default HotelCard; 